var interfaceForXlsImportWin;
var companyStore;
var interfaceForXlsSelectStore;
var exClassItemsIdStore;
var empStore;
var socialStore;
var exClassIdStore;

Ext.onReady(function() {
	interfaceForXlsImportWin = interfaceForXlsImport();

	var interfaceForXlsSelectPanel = new Ext.form.FormPanel({

		frame : true,
		hideLables : false,
		id : 'addPanelForm',
		buttonAlign : 'right',
		bodyStyle : 'padding:5px 5px 0',
		items : [{
					layout : 'column',
					items : [{
								columnWidth : .5,
								layout : 'form',
								items : [{
											layout : 'form',
											xtype : 'label',
											id : 'company_chinese_code',
											text : '客户中文名称 ：'
													+ session_companyName
										}]
							}, {
								columnWidth : .5,
								layout : 'form',
								items : [{
											xtype : 'textfield',
											fieldLabel : '接口名称',
											name : 'exNameSelect',
											id : 'exNameSelect',
											anchor : '90%'
										}]

							}]

				}],
		buttons : [{
			text : '查询',
			handler : function() {
				interfaceForXlsSelectStore.on('beforeload', function() {
							interfaceForXlsSelectStore.baseParams = {
								exNameSelect : interfaceForXlsSelectPanel
										.findById('exNameSelect').getValue()
							}
						});
				interfaceForXlsSelectStore.reload();
			}
		}, {
			text : '新增',
			handler : function() {
				modifyInterfaceInfo(null, null, null, null, null);
			}
		}, {
			text : '删除',
			handler : function() {
				var length = interfaceForXlsSelectGrid.getSelectionModel()
						.getSelections().length
				if (length < 1) {
					Ext.Msg.alert('警告', '请选择需要删除的项目');
					return;
				}
				Ext.MessageBox.show({
					title : '提示',
					msg : '您选中了' + length + '条记录进行删除,请问您确认吗?',
					buttons : Ext.MessageBox.OKCANCEL,
					fn : function(btn) {
						if (btn == "ok") {
							var exIntefaceIds = [];
							var rs = interfaceForXlsSelectGrid
									.getSelectionModel().getSelections();
							for (var i = 0; i < length; i++) {
								var exInterfaceId = rs[i].data.exInterfaceId;
								exIntefaceIds.push(exInterfaceId);
							}
							Ext.Ajax.request({
								url : "ExeclCustomize!deleteExcelIntefaceMaster.action?exIntefaceIds="
										+ Ext.encode(exIntefaceIds),
								method : "post",
								success : function(response, options) {
									interfaceForXlsSelectStore.reload();
									Ext.Msg.alert('提示', '删除操作成功！');
								}
							});
						}
						if (btn == "cancel") {
							return;
						}
					}
				});

			}
		}]

	});

	// 子表显示
	// ExeclCustomize!queryPublicMasterByCondition.action

	interfaceForXlsSelectStore = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
					url : 'ExeclCustomize!queryInterfaceMasterByCondition.action'
				}),
		reader : new Ext.data.JsonReader({
					root : 'publicInteface.result',
					totalProperty : 'publicInteface.totalCount'
				}, [{
							name : 'exInterfaceId',
							mapping : "exInterfaceId"
						}, {
							name : 'exPublicType',
							mapping : "exPublicType"
						}, {
							name : 'exName',
							mapping : "exName"
						}, {
							name : 'exClassId',
							mapping : "exClassId"
						}, {
							name : 'exEmpId',
							mapping : 'exEmpId'
						}, {
							name : 'exEmpIdCol',
							mapping : "exEmpIdCol"
						}, {
							name : 'exStart',
							mapping : 'exStart'
						}, {
							name : 'exEnd',
							mapping : 'exEnd'
						}, {
							name : 'memo',
							mapping : 'memo'
						}, {
							name : 'payClassName',
							mapping : 'payClassName'
						}])
	});
	interfaceForXlsSelectStore.load();

	// 查询显示的表格
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
		header : '名称',
		dataIndex : 'exName',
		width : 150,
		renderer : function(value, p, record) {
			var param = [];
			return "<a href='#' onclick=\"modifyInterfaceInfo('"
					+ record.data.exInterfaceId + "','"
					+ record.data.exPublicType + "','" + record.data.exName
					+ "','" + record.data.exClassId + "','" + record.data.memo
					+ "');return false;\">" + value + "</a>";

		}
	}, {
		header : 'excel文件类别',
		dataIndex : 'exPublicType',
		width : 150,
		renderer : function(v, p, record) {
			if (v == 1013) {
				return '雇员信息';
			} else if (v == 1015) {
				return '薪资类别';
			} else if (v == 1017) {
				return '社保项目';
			}
		}
	}, {
		header : '对应Id号',
		dataIndex : 'exClassId',
		width : 150,
		renderer : function(v, p, record) {
			return record.data.payClassName;
		}
	}, {
		header : '数据开始行',
		dataIndex : 'exStart',
		width : 150
	}]);
	interfaceForXlsSelectPanel.render(Ext.getBody());
	var interfaceForXlsSelectGrid = new Ext.grid.GridPanel({
				height : document.body.clientHeight
						- Ext.getCmp('addPanelForm').getInnerHeight()
						- Ext.getCmp('addPanelForm').getFrameHeight(),

				id : 'interfaceForXlsSelectGrid',
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : interfaceForXlsSelectStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录'
						}),
				cm : cm,
				sm : sm,
				store : interfaceForXlsSelectStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				}
			});

	interfaceForXlsSelectGrid.render(Ext.getBody());
});

function modifyInterfaceInfo(exInterfaceId, exPublicType, exName, exClassId,
		memo) {
	interfaceForXlsImportStore.proxy = new Ext.data.HttpProxy({
				url : 'ExeclCustomize!queryIntefaceDetail.action'
			});
	interfaceForXlsImportStore.baseParams = {
		exInterfaceId : exInterfaceId != null ? exInterfaceId : -1
	}
	interfaceForXlsImportStore.modified = [];
	interfaceForXlsImportStore.reload();

	interfaceForXlsImportWin.findById('exInterfaceId').setValue(exInterfaceId);
	interfaceForXlsImportWin.findById('exName').setValue(exName);
	interfaceForXlsImportWin.findById('exPublicType').setValue(exPublicType);
	// 导入类别为雇员信息
	if (exPublicType == 1013) {
		empStore.reload();
		interfaceForXlsImporttGrid.reconfigure(interfaceForXlsImportStore,
				exSubNamecm);
	}
	// 导入类别为薪资类别
	if (exPublicType == 1015) {
		exClassItemsIdStore.baseParams = {
			payClassId : exClassId
		}
		exClassItemsIdStore.reload();
		// exClassIdStore.reload();
		interfaceForXlsImporttGrid.reconfigure(interfaceForXlsImportStore,
				payclasscm);
	}
	// 导入类别为社保项目
	if (exPublicType == 1017) {
		socialStore.reload();
		interfaceForXlsImporttGrid.reconfigure(interfaceForXlsImportStore, cm);
	}
	// selectByType(exPublicType);
	interfaceForXlsImportWin.findById('exClassId').setValue(exClassId == 'null'
			? ''
			: exClassId);
	interfaceForXlsImportWin.findById('memo').setValue(memo == 'null'
			? ''
			: memo);
	if (exInterfaceId == null) {
		Ext.getCmp('exPublicType').setDisabled(false);
		Ext.getCmp('exName').setDisabled(false);
		Ext.getCmp('deleteItems').setDisabled(true);
		Ext.getCmp('addItems').setDisabled(true);
	} else {
		Ext.getCmp('exPublicType').setDisabled(true);
		Ext.getCmp('exName').setDisabled(true);
		Ext.getCmp('addItems').setDisabled(false);
		Ext.getCmp('deleteItems').setDisabled(false);
		Ext.getCmp('addItems').setDisabled(false);
	}

	interfaceForXlsImportWin.show();
}
